#include "../includes.h"


//#define ODETIC_COMMAND_NO	27
//#define ODETIC_COMMAND_NO	28
//#define ODETIC_COMMAND_NO	29


#define ALL_			  	0
#define AGPS_				1
#define ANT1_				2
#define ANTCHECK_			3
#define AZEL_				4
#define BEEP_				5
#define CTOD_				6
#define DACP_				7
#define EFER_				8
#define ESSD_				9
#define ESSN_				10
#define ESTC_				11
#define GPSE_				12
#define LEAP_				13
#define PAVG_				14
#define PMD1_				15
#define POS1_				16
#define PRID_				17
#define RSTG_				18
#define SIGP_				19
#define SIGQ_				20
#define TEMP_				21
#define TIMD_				22
#define TIME_				23
#define TMD1_				24
#define TMP1_				25
#define MANI_				26
#define UNSL_				27
#define VERS_				28
#define RSTB_				29
#define MONITOR_			30
#define FWDN_				31
#define GRCV1_				32

Odtcmd_	Odtcmd[ODETIC_COMMAND_NO] = {
	{"AGPS"		,1		,1},
	{"ANT1"		,2		,10},
	{"ANTCHECK"	,3		,0},
	{"AZEL"		,4		,5},
	{"BEEP"		,5		,0},
	{"CTOD"		,6		,1},
	{"DACP"		,7		,1},
	{"EFER"		,8		,1},
	{"ESSD"		,9		,1},
	{"ESSN"		,10		,1},
	{"ESTC"		,11		,10},
	{"GPSE"		,12		,0},
	{"LEAP"		,13		,0},
	{"PAVG"		,14		,1},
	{"PMD1"		,15		,10},
	{"POS1"		,16		,1},
	{"PRID"		,17		,0},
	{"RSTG"		,18		,0},
	{"SIGP"		,19		,1},
	{"SIGQ"		,20		,1},
	{"TEMP"		,21		,5},
	{"TIMD"		,22		,1},
	{"TIME"		,23		,1},
	{"TMD1"		,24		,5},
	{"TMP1"		,25		,5},
	{"MANI"		,26		,10},
	{"UNSL"		,27		,0},
	{"VERS"		,28		,0},
	{"RSTB"		,29		,0},
	{"MONITOR"	,30		,0},
	{"FWDN"		,31		,0},
	{"GRCV1"	,32		,0}
};


/*
typedef struct {

	char ctrl[ODETIC_COMMAND_NO+1];
	char common[ODETIC_COMMAND_NO+1];

} unsl_flag_;
*/

unsl_flag_ unsl_flag;

OdtAutoSend_	AutoReply;


void Init_OdeticCommand();
CMD_ OdeticGetCommandID(unsigned char *Buffer,int length);
char * OdtCmdReply(int,char*,char);


void AutoSendSet();
void AutoSendCommand(char port);


char * gps_pos_hold_p_odt(char *parameter);
char * gps_pos_mode_p_odt(char *parameter);


char *	set_dac(char *);
char *	set_time_mode(char *);
char * 	get_track_status(void);


char time_adelay_p(char*parameter);


void Init_OdeticCommand()
{
	int i;
	unsl_flag.ctrl[ALL_]	= ON;
	unsl_flag.common[ALL_]	= ON;

	for( i = 1; i <= ODETIC_COMMAND_NO; i++ ){
		unsl_flag.ctrl[i] = OFF;
		unsl_flag.common[i] = OFF;
	}
	//EnableTODPortTx();
}



void AutoSendSet()
{
	int	modresult;

	AutoReply.interval_2 = AutoReply.interval_5 = AutoReply.interval_10 = OFF;

	// 1,2,3,4,5,6,7,8,9,10
	if(SYSTEM.TIME.Valid) {
		modresult = GC.ACCsecond%10;
	}
	else {
		modresult = GC.RunningTime%10;
	}

	if(!(modresult&0x00000001)) {
		AutoReply.interval_2 = ON;
	}

	if(modresult == 5 || modresult == 0){
		AutoReply.interval_5 = ON;
	}

	if(modresult == 0){
		AutoReply.interval_10 = ON;
	}


	AutoReply.CmdNo_CTRL = 0;
	AutoReply.CmdNo_COMMON = 0;
	AutoReply.ctrl_flag = ON;
	AutoReply.common_flag = ON;
}



void AutoSendCommand(char port)
{
	int i;
	char* reply;
	char null_ch;
	
	null_ch = 0;
//	char message[40];

	
	if(port == PORT_CONTROL){
		if(unsl_flag.ctrl[AutoReply.CmdNo_CTRL+1] == ON){
			if( (Odtcmd[AutoReply.CmdNo_CTRL].FREQ == 1) ||
				(AutoReply.interval_2 && (Odtcmd[AutoReply.CmdNo_CTRL].FREQ ==2)) ||
				(AutoReply.interval_5 && (Odtcmd[AutoReply.CmdNo_CTRL].FREQ ==5)) ||
				(AutoReply.interval_10 && (Odtcmd[AutoReply.CmdNo_CTRL].FREQ ==10)) 	){

				//TransmitMessage((BYTE*)OdtCmdReply(AutoReply.CmdNo_CTRL+1,NULL,port),port);				
				TransmitMessage((BYTE*)OdtCmdReply(AutoReply.CmdNo_CTRL+1,&null_ch,port),port);				
				//reply = OdtCmdReply(AutoReply.CmdNo_CTRL+1,NULL,port);
				//if(reply != NULL)
				//{
					//TransmitMessage((BYTE*)reply,port);
				//}
			}
		}

		AutoReply.CmdNo_CTRL++;

	}

	if(AutoReply.CmdNo_CTRL == ODETIC_COMMAND_NO) {
		AutoReply.ctrl_flag = OFF;
	}



	if(port == PORT_COMMON){
		//if(tod_tx_ready == 0 && (TimeSliceCounter<3 || TimeSliceCounter>6))
		//kang if(tod_tx_ready == 0)
		if(1)
		{

		//while(AutoReply.CmdNo_COMMON != ODETIC_COMMAND_NO) {

			if(unsl_flag.common[AutoReply.CmdNo_COMMON+1] == ON){
				if( (Odtcmd[AutoReply.CmdNo_COMMON].FREQ == 1) ||
					(AutoReply.interval_2 && (Odtcmd[AutoReply.CmdNo_COMMON].FREQ ==2)) ||
					(AutoReply.interval_5 && (Odtcmd[AutoReply.CmdNo_COMMON].FREQ ==5)) ||
					(AutoReply.interval_10 && (Odtcmd[AutoReply.CmdNo_COMMON].FREQ ==10)) 	){
						
					//TransmitMessage((BYTE*)OdtCmdReply(AutoReply.CmdNo_COMMON+1,NULL,port),port);
					TransmitMessage((BYTE*)OdtCmdReply(AutoReply.CmdNo_COMMON+1,&null_ch,port),port);
					//reply = OdtCmdReply(AutoReply.CmdNo_COMMON+1,NULL,port);
					//if(reply != NULL)
					//{
					//	TransmitMessage((BYTE*)reply,port);
					//}
				}
			}
			AutoReply.CmdNo_COMMON++;
		}

		//AutoReply.common_flag = OFF;
		//func.autoreply_task
		if(AutoReply.CmdNo_COMMON == ODETIC_COMMAND_NO) {
			//AutoReply.common_flag = OFF;
			func.autoreply_task = OFF;
		}
	}
}



void OdeticCommand(char port)
{

    int 			i,index;
    unsigned char 	CommandFound;

    int 			*RxCount;
    unsigned char 	*CommandBuffer;

    Command_	*Command;

	CMD_ CommandID;

    unsigned char 	SingleCommandBuff[SINGLE_COMMAND_LENGTH+1];


	switch(port){

		case PORT_CONTROL:
				Command = &COMMAND.CTRL_PORT;
				RxCount = &scc3_rx_cnt;
				CommandBuffer = CommandBuffer3;
				break;
		case PORT_COMMON:
		default:
				Command = &COMMAND.COMMON_PORT;
				RxCount = &scc1_rx_cnt;
				CommandBuffer = CommandBuffer1;
				break;
	}

	
	for( i = 0, CommandFound = NO; i < *RxCount; i++){
        if(CommandBuffer[i] == '$'){
        	CommandFound = 1;
        }
        if (CommandBuffer[i] == '*' && CommandFound == 1){
            if( i > 0){
            	//CommandBuffer[i+1] = NULL;	CommandFound = 2;
            	CommandFound = 2;
            } else {
				*RxCount = 0;
            }
            break;
        }
    }
    if ( CommandFound == 2 && *RxCount != 0 ){
    	
		// (1)
		Command->SERVICE = CMD_PARSING;
		Command->Output[0] = (char)0;
        Command->Count = 0;

        for( i = 0, index = 0; i < *RxCount && Command->Count < MAX_COMMAND_CASCADE; i++ ){
			//SingleCommandBuff[index++] = CommandBuffer[i];

//            if( (i > 1) && (CommandBuffer[i-1] == 0x0D || CommandBuffer[i] == 0x0A)){
            //if( (i > 2) && CommandBuffer[i-2] == '*' && (CommandBuffer[i-1] == 0x0D || CommandBuffer[i] == 0x0A)){
             if( (i > 2) && (CommandBuffer[i-1] == '*') && (CommandBuffer[i] == 0x0A)){
                SingleCommandBuff[index] = (unsigned char)0;
		        //CommandID = OdeticGetCommandID(SingleCommandBuff,strlen((char*)SingleCommandBuff));
		        //CommandID = OdeticGetCommandID(SingleCommandBuff,index);// ��ȣ ��
				CommandID = OdeticGetCommandID(SingleCommandBuff,strlen((char*)SingleCommandBuff));

		       	if (CommandID.ID != 0 && CommandID.ID != -1){
        	    // (3)
        	    
	        	  	CommandID.Port = port;
    	       		Command->Queue[Command->Count++] = CommandID;
				}

//				sprintf((char*)MESG,"-> %s %d %d\r\n",SingleCommandBuff,CommandID.ID,CommandID.CommandSet);
//				TransmitMessage((char*)MESG,PORT_CONTROL);

	       		//}
	       		index = 0;
	       		break;
            }
            else {
                if( index < SINGLE_COMMAND_LENGTH -1 ){
                    SingleCommandBuff[index++] = CommandBuffer[i];
                }
            }
		}
//    	printf("$$%d,%d ,%d, %d\n",CommandFound,*RxCount,index,Command->Count);

        *RxCount = 0;

		if(Command->Count == 0){
			Command->SERVICE = CMD_REPLY;
		}
		else{
			Command->Current = 0;
			Command->Output[0] = (char)0;
			Command->SERVICE = CMD_EXECUTION;
		}
    }
    else {
   		Command->SERVICE = CMD_PRE_INPUT;
    }

    if(*RxCount >= SCC_BUF_LENGTH-1){
      	*RxCount = 0;
    }
}



CMD_ OdeticGetCommandID(unsigned char *Buffer,int length)
{
    int 	i,j,found;
    int 	str_index ;
    char    CommandPart;

    char 	Command[20];

	CMD_	result;

    str_index = 0;
    CommandPart = COMMAND_INDICATOR;

    Command[0] = (char)0;

    result.CommandSet = Odetic_COMMAND;
    result.Parameter[0] = (char)0;
    result.ID = UERROR;
	result.CallCount = 0;

//	Buffer[length]=0; printf(Buffer);
//	printf("$$ $1%d\n",length);
    for(i = 0; i <= length; i++){

        switch( CommandPart ){

            case    COMMAND_INDICATOR:

                        if(Buffer[i] == '$'){
                        	CommandPart = COMMAND_STRING;
                        }
                        break;

            case    COMMAND_STRING:

                        if(isalpha((int)Buffer[i]) || isdigit((int)Buffer[i]) ){
                            if(str_index > ODETIC_COMMAND_MAX_LENGTH) {
                            	return result;
                            }
                            Command[str_index++] = upper((char)Buffer[i]);
                        }
                        else if (Buffer[i] == ','){   /* parameter */
                            Command[str_index] = (char)0;
                            CommandPart = COMMAND_PARAMETER;
                        }
                        else if (Buffer[i] == '*'){ /* command end */
                            Command[str_index] = (char)0;
                            CommandPart = COMMAND_END;
                        }
                        else{
                            return result;
                        }

                        break;

            case    COMMAND_PARAMETER:
            case    COMMAND_END:

                        found = NO;
                        for( j = 0; j < ODETIC_COMMAND_NO ; j++ ){

                        	if( !strcmp(Odtcmd[j].NAME,Command) ){
                        		found = YES;
                        		result.ID = Odtcmd[j].ID;
                        		break;
                        	}
                        }

                        if(found){
							result.Parameter[0] = (char)0;
                            if (CommandPart == COMMAND_PARAMETER){

                                for(j = 0; i < length-1; j++,i++)  {
                                	if( j < 64 ){
                                		result.Parameter[j] = upper(Buffer[i]);
                                	}
                                }
                                result.Parameter[j] = (char)0;
                            }
                        }

                        return result;

                        break;
        }
    }
    return result;
}


char DoOdeticCommand(int comm, char *Parameter, char count, char port)
{
	int 	length;

	char message[1024];

	char result;

    Command_	*Command;

	switch(port){

		case PORT_CONTROL:
				Command = &COMMAND.CTRL_PORT;
				break;
		case PORT_COMMON:
		default:
				Command = &COMMAND.COMMON_PORT;
				break;
	}

	message[0] = (char)0;

	strcpy(message,OdtCmdReply(comm,Parameter,port));

	length = strlen(message);

    if( length > 0){

   		//if( (strlen(Command->Output) + length) < 1024 ){
   		if( (strlen(Command->Output) + length) < CMD_PARAMETER_LENGTH ){
    		strcat(Command->Output,message);
   		}
   	}

    return EXE_END;
}


extern char first_lock_reckon;

char odt_buf[512];
char * OdtCmdReply(int comm,char *Parameter,char port)
{
	int 	t1,t2,t3,l1,l2,l3;
	float   f1,f2,f3,f4;
	long    k1,k2,k3;
	char    d1,d2;
	int 	i,j,length;
	
	char flag,found;

	//char string[20];
	char string[40];
	//char message[256];
	//char message[512];
	char* message;
	//char* pmessage;

	char result;
	char* str_model;
	char* str_manu;
	char* str_date;
	char str_ver[40];
	
	time_ ReplyTime;
	
	message = odt_buf;
	message[0] = (char)0;
	string[0] = (char)0;

	result = EXE_END;

	switch(comm){
		
		case AGPS_:
						sprintf(message,"$AGPS,1*");
						break;


		case ANT1_:
						if(strlen(Parameter)>0){
						//if(Parameter != (char)0){
							if(time_adelay_p(Parameter) == EXE_END ){
								sprintf(message,"$ANT1,%06ld*",CONFIG.ANTDLY);
							}
						}
						else {
							sprintf(message,"$ANT1,%06ld*",CONFIG.ANTDLY);
						}
						break;

						
		case ANTCHECK_:
						if(strlen(Parameter)>0){
						//if(Parameter!=NULL){

							if(Parameter[0] == '1'){
								d1 = '1';
								CONFIG.openshort_flag = ON;
								CONFIG.AntCheckFlag = YES;
								CONFIG.WriteEvent = ON;
							}
							else if(Parameter[0] == '0'){
								d1 = '0';
								CONFIG.openshort_flag = OFF;
								CONFIG.AntCheckFlag = NO;
								CONFIG.WriteEvent = ON;
								//DisableBeep();
							}
							else
							{
								if(CONFIG.AntCheckFlag == ON) {d1 = '1';}
								else {d1 = '0';}
							}
						}
						else{
							if(CONFIG.AntCheckFlag == ON){d1 = '1';}
							else {d1 = '0';}
						}

						sprintf(message,"$ANTCHECK,%c*",d1);
						break;
						

		case AZEL_:
						sprintf(message,"$AZEL");
							for( i = 0; i < 12; i++ ){
							sprintf(string,",%.2d,%.2d,%.3d",SYSTEM.TRACK.VIS_SAT[i].ID,SYSTEM.TRACK.VIS_SAT[i].EL,SYSTEM.TRACK.VIS_SAT[i].AZ);
							strcat(message,string);
						}
						strcat(message,"*");
						break;
						
						
		case BEEP_:
						if(strlen(Parameter)>0){
						//if(Parameter!=NULL){

							if(Parameter[0] == '1'){
								d1 = '1';
								CONFIG.beep_flag = ON;
								CONFIG.WriteEvent = ON;
							}
							else if(Parameter[0] == '0'){
								d1 = '0';
								CONFIG.beep_flag = OFF;
								CONFIG.WriteEvent = ON;
								//DisableBeep();
							}
							else
							{
								if(CONFIG.beep_flag == ON) {d1 = '1';}
								else {d1 = '0';}
							}
						}
						else{
							if(CONFIG.beep_flag == ON){d1 = '1';}
							else {d1 = '0';}
						}

						sprintf(message,"$BEEP,%c*",d1);
						break;					
		
		
		
		case CTOD_:		// $VERS,R1.2.00,Jan 19 2000,3.1,000512-1-0*27
						strcpy(message,"$CTOD,A,B,C,D,E,F,G,H*");
						message[6] = hex_char[(R_TOD.status>>4) 	& 0xF];
						message[8] = hex_char[ R_TOD.status 		& 0xF];
						message[10] = hex_char[(R_TOD.error>>4) 	& 0xF];
						message[12] = hex_char[ R_TOD.error 		& 0xF];
						message[14] = hex_char[(R_TOD.opmode>>4) 	& 0xF];
						message[16] = hex_char[ R_TOD.opmode 		& 0xF];
						message[18] = hex_char[(R_TOD.inform>>4) 	& 0xF];
						message[20] = hex_char[ R_TOD.inform 		& 0xF];
						break;
						
						
		case DACP_:     // dac of primary oscillator
						if(strlen(Parameter)>0){
						//if(Parameter!=NULL){
		                    if(USER.MANUAL_CONTROL == ON){
        		            	if(sscanf(Parameter,"%f",&f1) == 1){
		        		        	MakePWMInterval(f1+OCXO_ID.TDAC);
									sprintf(message,"$DACP,%d*",CONTROL.CurrentDAC);
								}
							}
						}
						else {
							sprintf(message,"$DACP,%d*",CONTROL.CurrentDAC);
						}
						break;
						
						

		case EFER_:     // Frequency
						sprintf(message,"$EFER,%+04.2E*",CONTROL.Frequency);
						break;


		case ESSD_:
						sprintf(message,"$ESSD,%+04.2E*",CONTROL.AvgTimeOffset);
						break;
						
						
		case ESSN_:
						sprintf(message,"$ESSN,%010d*",GC.RunningTime);
						break;


		case ESTC_:
						sprintf(message,"$ESTC,%07d*",(int)GC.RunningTime/86400);
						break;


		case GPSE_:
						// GPS receiver info.
						// 1: Motorola
						// 3: Motorola UT+
						// 5: navicom
						// 7~9: N/A
						if(SYSTEM.EB.STATUS == EB_STATUS_NONE){
							t1 = 9;
							t2 = 8;
							t3 = 0;
						}
						else {
							if(SYSTEM.EB.ID == UT_PLUS_ONCORE ){
								t1 = 3;
								t2 = 8;
							}
							else if(SYSTEM.EB.ID == SYNC_15 ){
								t1 = 5;
								t2 = 8;
							}
							else{
								t1 = 9;
								t2 = 8;
							}
							t3 = 1;
						}
						sprintf(message,"$GPSE,%d,%d,%d*",t1,t2,t3);
						break;
						

		case LEAP_:     // leapsecond
						sprintf(message,"$LEAP,%d*",SYSTEM.TIME.LEAPSECOND);
						break;
    	
						//if( LOCK_REFERENCE && (REFERENCE_MODE==EB_STATUS_RUN) && (SYSTEM.OPERATION.LOCKMODE != HOLDOVER_OVER10MIN) ){
						//	sprintf(message,"$LEAP,%02d*",SYSTEM.TIME.LEAPSECOND);
						//}
						//else {
						//	strcpy(message,"$LEAP,X*");
						//}
						//break;
						

		case PAVG_:     // average position
						t1 = SYSTEM.POSITION.AVERAGE.LAT/3600000;  d1 = (t1 >= 0) ? 'N':'S';
	    	            f1 = (float)(SYSTEM.POSITION.AVERAGE.LAT%3600000)/60000.0;

	            	    t2 = SYSTEM.POSITION.AVERAGE.LON/3600000;  d2 = (t2 >= 0) ? 'E':'W';
	                	f2 = (float)(SYSTEM.POSITION.AVERAGE.LON%3600000)/60000.0;

	                    f3 = (float)(SYSTEM.POSITION.AVERAGE.HEI/100.0);

	                    sprintf(message,"$PAVG,%.2d,%07.4f,%c,%.3d,%07.4f,%c,%07.1f*",t1,f1,d1,t2,f2,d2,f3);
						break;
						
						
		case PMD1_:
						strcpy(message,gps_pos_mode_p_odt(Parameter));
						message[4] = '1';
						if(message[0] == (char)0){
							result = EXE_ERROR;
						}
						break;
						
						

		case POS1_:
						strcpy(message,gps_pos_hold_p_odt(Parameter));
						if(message[0] == (char)0){
							result = EXE_ERROR;
						}
						break;
						


		case PRID_:     // product ID
						//kang sprintf(message,"$PRID,010,RCGD-M*");
						sprintf(message,"$PRID,010,GRCA*");
						break;
						
						

		case RSTG_:     // GPS receiver ��.
						if( CONFIG.RcvType ==1 ){
							if(Parameter[0] == 'C')
							{
								RequestEB(eDEFAULT);
								Init_EB(INIT_EB_LOUD, INIT_EB_SECOND, NO);
								EB_Task_start();
								strcpy(message,"$RSTG,C*");
							}
							else if(Parameter[0] == 'W')
							{
								Init_EB(INIT_EB_LOUD, INIT_EB_SECOND, NO);
								EB_Task_start();
								strcpy(message,"$RSTG,W*");
							}
						}
						if( CONFIG.RcvType ==2 ){
							if(Parameter[0] == 'C')
							{
							}
							else if(Parameter[0] == 'W')
							{
							}
						}
						else{
							if( strlen(Parameter) > 1 ){
								//printf("%d>>%s\n",strlen(Parameter),Parameter);
								sprintf(message,"$PUNV,START,%s",Parameter);
								SendNmeaToF1000c( message );
								sprintf(message,"$RSTG,%s*",Parameter);
							}
							else if(Parameter[0] == 'C')
							{
								//SendCommandToF1000c(nmeaResetCold);
								issue_f1000c_cold_start();
								//SendCommandToF1000c(nmeaResetAll);
								strcpy(message,"$RSTG,C*");
							}
							else if(Parameter[0] == 'W')
							{
								SendCommandToF1000c(nmeaResetWarm);
								strcpy(message,"$RSTG,W*");
							}
							else if(Parameter[0] == 'H')
							{
								SendCommandToF1000c(nmeaResetHot);
								strcpy(message,"$RSTG,H*");
							}
							else if(Parameter[0] == 'A')
							{
								SendCommandToF1000c(nmeaResetAll);
								strcpy(message,"$RSTG,A*");
							}
							else if(Parameter[0] == 'O')
							{
								SendCommandToF1000c(nmeaOutput);
								strcpy(message,"$RSTG,O*");
							}
							else if(Parameter[0] == 'R')
							{
								cmd_ResetRCV(0,NULL);
								strcpy(message,"$RSTG,R*");
							}
							else if(Parameter[0] == 'F')
							{
								SendNmeaToF1000c("$PUNV,START,FE7F*");
								printf("%d>>%s\n",strlen(Parameter),Parameter);
								strcpy(message,"$RSTG,F*");
							}
						}
						break;

		case SIGP_:
						strcpy(message,get_track_status());
						message[4] = 'P';
						break;
						
		case SIGQ_:
						strcpy(message,get_track_status());
						message[4] = 'Q';
						break;

		case TEMP_:
						sprintf(message,"$TEMP,%+04.1f*",TEMPERATURE.CURRENT);
						break;	
						

		case TIMD_:     // time difference between GPS 1pps and OCXO 1pps
						sprintf(message,"$TIMD,%+d*",(int)(CONTROL.AvgTimeOffset*BILLION));
						break;


		case TIME_:     // Time stamp
						//warmup - 0
						//time lock - 1
						//coasting - 2
						//recovering - 3
						if( first_lock_reckon || (GC.RunningTime > 30*60) ){
							if(TOD.status & BIT_STATUS_HOLD_OVER){
								d2 = 2; 	// coasting
							}
							else if( DIAG.TFOM > 4 ){
								d2 = 3;	// recovery
							}
							else {
								d2 = 1;  // time locked
							}

						}
						else {
							d2 = 0;	// warm-up
						}
						
						
						for( i = 0, t1 = (int)SYSTEM.TIME.CUR.DAY; i < SYSTEM.TIME.CUR.MONTH - 1; i++){
        					t1 += month[i];
    					}
						d1 = (SYSTEM.TIME.TIMEMODE == TIMEMODE_UTC) ? 'U':'G';

						sprintf(message,"$TIME,%.4d,%.3d,%.2d,%.2d,%.2d,%c,%c,%d*",
								SYSTEM.TIME.CUR.YEAR,
								t1,
								SYSTEM.TIME.CUR.HOUR,
								SYSTEM.TIME.CUR.MINUTE,
								SYSTEM.TIME.CUR.SECOND,
								d1,
								hex_char[DIAG.TFOM],
								d2
								);
						break;


		case TMD1_:     // time mode
						if(strlen(Parameter)>0){
						//if(Parameter!=NULL){

							if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
							// Set Time Mode
							if( Parameter[0] == 'G' ) {

								SYSTEM.TIME.TIMEMODE = TIMEMODE_GPS;
								CONFIG.TimeMode = TIMEMODE_GPS;
								CONFIG.WriteEvent = ON;

								strcpy(message,"$TMD1,G*");
							}
							else if( Parameter[0] == 'U' ) {

								SYSTEM.TIME.TIMEMODE = TIMEMODE_UTC;
								CONFIG.TimeMode = TIMEMODE_UTC;
								CONFIG.WriteEvent = ON;

								strcpy(message,"$TMD1,U*");
							}
							else {
								result = EXE_ERROR;
							}
						}
						else {
							d1 = (SYSTEM.TIME.TIMEMODE == TIMEMODE_UTC) ? 'U':'G';
							sprintf(message,"$TMD1,%c*",d1);
						}
						break;


		case TMP1_:     
						sprintf(message,"$TMP1,%+04.1f*",TEMPERATURE.CURRENT);
						break;



		case MANI_:
						// manufacture
						sprintf(message,"$MANI,NAVICOM*");
						break;


		case UNSL_:		// Unsolicited ...

						found = NO;
						j = strlen(Parameter);
						for(i = 0; i < j; i++ ){
							if( Parameter[i] == ','){

								d1 = Parameter[i+1];	Parameter[i] = (char)0;

								if(d1 == '1'){ 		flag = ON;}
								else if( d1 == '0'){flag = OFF;}
								else{				flag = UNDEFINED;}

								if( flag != UNDEFINED ){

									if(!strcmp(Parameter,"ALL")){

										if(port == PORT_CONTROL){
											for( i = 1; i <= ODETIC_COMMAND_NO; i++ ){
												unsl_flag.ctrl[i] = flag;
											}
										}
										else {
											for( i = 1; i <= ODETIC_COMMAND_NO; i++ ){
												unsl_flag.common[i] = flag;
											}
										}
										found = YES;
									}
									else {
										for(i = 0; i < ODETIC_COMMAND_NO; i++){
				                        	if( !strcmp(Odtcmd[i].NAME,Parameter) ){
												if(port == PORT_CONTROL){
													unsl_flag.ctrl[i+1] = flag;
												}
												else {
													unsl_flag.common[i+1] = flag;
												}
				                        		found = YES;
		    		                    	}
										}
									}
								}
							}
						}

						if(found){
							sprintf(message,"$UNSL,%s,%c*",Parameter,d1);
						}
						else {
							//result = EXE_ERROR;
							result = EXE_ERROR;
						}
						break;
						
		case VERS_:
						//sprintf(message,"$VERS,%s,%s,%s,01-00-01*",
						//		FirmWare_Version,Build_date,Rcv_Version);
						if(SYSTEM.EB.ID == UNDEFINED){
							strcpy(str_ver,"0.00");
						}
						else{
							sprintf(str_ver,"%s.%s", SYSTEM.EB.SWVER,SYSTEM.EB.SWREV);
						}
						
						sprintf(message,"$VERS,%s,%s,%s,01-00-01*",
								FirmWare_Version,Build_date,str_ver);
						break;
		case RSTB_:
						sprintf(message,"$RSTB,NAVICOM,RESET*");
						//kang U1puts(message);
						reset_sub_call();
						break;
		case MONITOR_:
						break;
		case FWDN_:
						sprintf(message,"$FWDN,NAVICOM,KERNEL*");
						length = strlen(message);
                        
    					for( i = 1, d1 = 0; i < length-1; i++ ){	d1 ^= message[i];	}
                        
						message[length]   =	hex_char[(d1>>4) & 0xF];
						message[length+1] = hex_char[     d1 & 0xF];
						message[length+2] = 0x0D;
						message[length+3] = 0x0A;
						message[length+4] = (char)0;
						
						//kang U1puts(message);
						for( j = 1; j <= ODETIC_COMMAND_NO; j++ ){
							unsl_flag.ctrl[j] = OFF;
							unsl_flag.common[j] = OFF;
						}
						for(j=0;j<10;j++) {
							((char *)0x000100)[j] = j;
						}
						reset_sub_call();
						break;
		case GRCV1_:	
						if(SYSTEM.EB.ID == SYNC_15){
							str_manu = "NAVICOM";
							str_date = SYSTEM.EB.SWDATE;
			 				
			 				if(SYSTEM.EB.SUBID == SYNC15_N){str_model = "SYNC-15N";}
			 				else if(SYSTEM.EB.SUBID == SYNC15_T){str_model = "SYNC-15T";}
			 				else{str_model = "SYNC-15";}
			 					
			 				sprintf(str_ver,"V%s.%s", SYSTEM.EB.SWVER,SYSTEM.EB.SWREV);
						}
						else if(SYSTEM.EB.ID == UT_PLUS_ONCORE){
							str_manu = "MOTOROLA";
							str_date = SYSTEM.EB.SWDATE;
							str_model = "UTPLUS";
							
							sprintf(str_ver,"V%s.%s", SYSTEM.EB.SWVER,SYSTEM.EB.SWREV);
							
						}
						else{
							str_manu = "0";
							str_model = "0";
							str_date = "0";
							strcpy(str_ver,"V0.00");
						}
						
						sprintf(message,"$GRCV1,%s,%s,%s,%s*",
								str_model,str_manu,str_ver,str_date);
						break;
		case 0:
		case -1:
						result = EXE_ERROR;
						break;

	}

    length = strlen(message);

    if(length == 0 || length > 510){
    	strcpy(message,"$ERRX,0001,Bad user message*");
	    length = strlen(message);
	}

	for( i = 1, d1 = 0; i < length-1; i++ ){	d1 ^= message[i];	}

	message[length]   =	hex_char[(d1>>4) & 0xF];
	message[length+1] = hex_char[     d1 & 0xF];
	message[length+2] = 0x0D;
	message[length+3] = 0x0A;
	message[length+4] = (char)0;

	return message;
	
}



char * gps_pos_mode_p_odt(char *parameter)
{
	char	d1, d2;

	ret_buf[0] = (char)0;

	if(strlen(parameter)>0){
	//if(parameter!=NULL){
		
		if(parameter[0] == 'K'){

			if( SYSTEM.POSITION.CURRENT.LAT != 0  && SYSTEM.POSITION.CURRENT.LON != 0 && SYSTEM.POSITION.CURRENT.HEI != 0){

				SYSTEM.EB.PHP.POSITION = SYSTEM.POSITION.CURRENT;
				SYSTEM.EB.PHP_Enable = ON;

				CONFIG.PositionMode = POSITION_MODE_KNOWN;
	       		CONFIG.WriteEvent = ON;

				strcpy(ret_buf,"$PMDP,K*");
			}
		}
		else if(parameter[0] == 'P'){
			if( SYSTEM.POSITION.PositionMode == POSITION_MODE_KNOWN){
				SYSTEM.EB.PHM_Disable = ON;
			}
			Start_Position_Survey(0);
			CONFIG.PositionMode = POSITION_MODE_AVERAGING;
       		CONFIG.WriteEvent = ON;

			strcpy(ret_buf,"$PMDP,P*");
		}

		else if(parameter[0] == 'D'){
			if( SYSTEM.POSITION.PositionMode == POSITION_MODE_KNOWN){
				SYSTEM.EB.PHM_Disable = ON;
			}
			SYSTEM.POSITION.PositionMode = POSITION_MODE_DYNAMIC;
			CONFIG.PositionMode = POSITION_MODE_DYNAMIC;
       		CONFIG.WriteEvent = ON;

			strcpy(ret_buf,"$PMDP,D*");
		}
	}
	else {

		switch(SYSTEM.POSITION.PositionMode){
			case POSITION_MODE_KNOWN:		d1 = 'K';	break;
			case POSITION_MODE_AVERAGING:	d1 = 'P';	break;
			case POSITION_MODE_DYNAMIC:		d1 = 'D';	break;
			default:						d1 = 'D';	break;
		}
		sprintf((char*)ret_buf,"$PMDP,%c*",d1);
	}

	return (char*)ret_buf;
}



char *	gps_pos_hold_p_odt(char *parameter)
{
	int 	t1,t2,t3,t4,l1,l2,l3;
	float   f1,f2,f3,f4;
	long    k1,k2,k3;
	char    d1,d2,rID;
	
	ret_buf[0] = (char)0;

	if(strlen(parameter)>0){
	//if(parameter != (char)0){

		if( (SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP) ||  (SYSTEM.EB.PHP_Enable == ON )	){
			return (char*)ret_buf;
		}
		else if( sscanf(parameter,"%d,%f,%c,%d,%f,%c,%f",&t1,&f1,&d1,&t2,&f2,&d2,&f3) == 7){
			if((t1 >= 0 && t1 < 90) && (f1 >= 0.0 && f1 < 60.0) && (d1 == 'N' || d1 == 'S') &&
				(t2 >= 0 && t2 < 180) && (f2 >= 0.0 && f2 < 60.0) && (d2 == 'E' || d2 == 'W') &&
				(f3 >= 0.0 && f3 < 100000.0))
			{
				// use current input position
				k1 = (long)t1*3600000+f1*60000.0;
				k1 = ( (d1 == 'N') || (d1 == 'n')) ? k1:-k1;
				k2 = (long)t2*3600000+f2*60000.0;
				k2 = ( (d2 == 'E') || (d2 == 'e')) ? k2:-k2;
				k3 = (long)(f3*100.0);
	
				SYSTEM.EB.PHP.POSITION.LAT = k1;
				SYSTEM.EB.PHP.POSITION.LON = k2;
				SYSTEM.EB.PHP.POSITION.HEI = k3;
	
				SYSTEM.EB.PHP_Enable = ON;
	
		        //sprintf((char*)MESG,"$POS1,%s",parameter);
		        sprintf((char*)ret_buf,"$POS1,%s",parameter);
		    }
		}
	}
	else {
        t1 = SYSTEM.POSITION.CURRENT.LAT/3600000;
		f1 = (float)(SYSTEM.POSITION.CURRENT.LAT%3600000)/60000.0;
		if( t1 >= 0 ) {
			d1 = 'N';
		}
		else {
			d1 = 'S';
		}
	
		t2 = SYSTEM.POSITION.CURRENT.LON/3600000;
		f2 = (float)(SYSTEM.POSITION.CURRENT.LON%3600000)/60000.0;
		if( t2 >= 0 ) {
			d2 = 'E';
		}
		else {
			d2 = 'W';
		}
	
		f3 = (float)(SYSTEM.POSITION.CURRENT.HEI/100.0);
		
		sprintf(ret_buf,"$POS1,%.2d,%07.4f,%c,%.3d,%07.4f,%c,%07.1f*",t1,f1,d1,t2,f2,d2,f3);
	}
	
	return (char*)ret_buf;
}




char time_adelay_p(char*parameter)
{
	long	k1;

	if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	return EXE_END;	}
	// Set Antenna Delay
    if( sscanf(parameter,"%ld",&k1) == 1){

        CONFIG.ANTDLY = k1;
        SetANTDLY(k1);
        CONFIG.WriteEvent = ON;


		if(FIRST_LOCK){
			RECOVERY.flag = ON;
			RECOVERY.IntervalCounter = 0;
			RECOVERY.wait_time = 10;
			RECOVERY.average_time = 20;
			RcvAlarm.skip_counter = 30;
		}

        return EXE_END;
	}
    else{
    	return EXE_ERROR;
    }
}


char *	set_dac(char *Parameter)
{
	float f1;

	ret_buf[0] = (char)0;

	if(strlen(Parameter)>0){
	//if(Parameter!=NULL){
		if(sscanf(Parameter,"%f",&f1) == 1){
			if(USER.MANUAL_CONTROL == ON){
				if(f1 >=0 && f1 <= 65535)
				{
					MakePWMInterval(f1+OCXO_ID.TDAC);
					sprintf((char*)ret_buf,"$DACP,%05d*",CONTROL.CurrentDAC);
				}
				else
				{
					sprintf((char*)ret_buf,"$DACP,X*");
				}
			}
			else
			{
				sprintf((char*)ret_buf,"$DACP,X*");
			}
		}
		else
		{
			sprintf((char*)ret_buf,"$DACP,%05d*",CONTROL.CurrentDAC);
		}
	}
	else
	{
		sprintf((char*)ret_buf,"$DACP,%05d*",CONTROL.CurrentDAC);
	}
	
	return ret_buf;
}



char * get_track_status(void)
{
	int i;
	int d1,d2;
	char string[100];
	
	ret_buf[0] = 0;
	strcpy(ret_buf,"$SIGP");

	for(i = 0; i < 8; i++){
		d1 = SYSTEM.TRACK.SAT[i].SS/6;
		d1 = (d1>9)?9:d1;
		if(SYSTEM.TRACK.SAT[i].SS>=1){	d2 = '1';      	}
		else {							d2 = '0';		}

		sprintf(string,",%02d,%d,%c",SYSTEM.TRACK.SAT[i].ID,d1,d2);
		strcat(ret_buf,string);
	}
    strcat(ret_buf,"*");

	return ret_buf;
}



char *set_time_mode(char *Parameter)
{

	char d1;

	ret_buf[0] = (char)0;

	if(strlen(Parameter)>0){
	//if(Parameter!=NULL){

		//if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
		// Set Time Mode
		if( Parameter[0] == 'G' ) {

			SYSTEM.TIME.TIMEMODE = TIMEMODE_GPS;
			CONFIG.TimeMode = TIMEMODE_GPS;
			CONFIG.WriteEvent = ON;

			strcpy(ret_buf,"$TMDP,G*");
		}
		else if( Parameter[0] == 'U' ) {

			SYSTEM.TIME.TIMEMODE = TIMEMODE_UTC;
			CONFIG.TimeMode = TIMEMODE_UTC;
			CONFIG.WriteEvent = ON;

			strcpy(ret_buf,"$TMDP,U*");
		}
		else
		{
			d1 = (SYSTEM.TIME.TIMEMODE == TIMEMODE_UTC) ? 'U':'G';
			sprintf((char*)ret_buf,"$TMDP,%c*",d1);
		}
	}
	else {
		d1 = (SYSTEM.TIME.TIMEMODE == TIMEMODE_UTC) ? 'U':'G';
		sprintf((char*)ret_buf,"$TMDP,%c*",d1);
	}

	return ret_buf;
}

